package com.laolang.shop.common.core.util;

import com.laolang.shop.common.core.consts.StatusCodeConst;
import com.laolang.shop.common.core.exception.BusinessException;

/**
 * @author khlbat
 * @version 1.0
 * @date 2020/11/1 20:46
 */
public class AssertUtil {

    private AssertUtil() {
    }

    public static <T> T notNull(T object, Integer code, String msg) {
        if (object == null) {
            throw new BusinessException(code, msg);
        }
        return object;
    }

    public static <T> T notNull(T object, String msg) {
        if (object == null) {
            throw new BusinessException(StatusCodeConst.FAILED.getCode(), msg);
        }
        return object;
    }

    public static <T> T notNull(T object, Integer code) {
        if (object == null) {
            throw new BusinessException(code, StatusCodeConst.FAILED.getMsg());
        }
        return object;
    }

    public static <T> T notNull(T object) {
        if (object == null) {
            throw new BusinessException(StatusCodeConst.FAILED.getCode(), StatusCodeConst.FAILED.getMsg());
        }
        return object;
    }


    public static <T> T isNull(T object, Integer code, String msg) {
        if (object == null) {
            throw new BusinessException(code, msg);
        }
        return object;
    }

    public static <T> T isNull(T object, String msg) {
        if (object == null) {
            throw new BusinessException(StatusCodeConst.FAILED.getCode(), msg);
        }
        return object;
    }

    public static <T> T isNull(T object, Integer code) {
        if (object == null) {
            throw new BusinessException(code, StatusCodeConst.FAILED.getMsg());
        }
        return object;
    }

    public static <T> T isNull(T object) {
        if (object == null) {
            throw new BusinessException(StatusCodeConst.FAILED.getCode(), StatusCodeConst.FAILED.getMsg());
        }
        return object;
    }

    public static void isTrue(boolean expression, Integer code, String msg) {
        if (!expression) {
            throw new BusinessException(code, msg);
        }
    }

    public static void isTrue(boolean expression, String msg) {
        if (!expression) {
            throw new BusinessException(StatusCodeConst.FAILED.getCode(), msg);
        }
    }

    public static void isTrue(boolean expression, Integer code) {
        if (!expression) {
            throw new BusinessException(code, StatusCodeConst.FAILED.getMsg());
        }
    }

    public static void isTrue(boolean expression) {
        if (!expression) {
            throw new BusinessException(StatusCodeConst.FAILED.getCode(), StatusCodeConst.FAILED.getMsg());
        }
    }
}

